//
//  PlayingAlbumCell.m
//  SuperRadio
//
//  Created by supercchome on 14/12/23.
//  Copyright (c) 2014年 三个臭皮匠. All rights reserved.
//

#import "PlayingAlbumCell.h"
#import "PlayingAlbum.h"
#import "UIImageView+WebCache.h"

@interface PlayingAlbumCell()

@property(nonatomic, retain)UIImageView *playImageView;
@property(nonatomic, retain)UILabel *nameLable;
@property(nonatomic, retain)UIImageView *timaImageView;
@property(nonatomic, retain)UILabel *timeLable;


@end

@implementation PlayingAlbumCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.playImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:self.playImageView];
        [_playImageView release];
        
        
        self.nameLable = [[UILabel alloc]init];
        [self.contentView addSubview:self.nameLable];
        [_nameLable release];
        
        
        self.timaImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:self.timaImageView];
        [_timaImageView release];
        
        
        self.timeLable = [[UILabel alloc]init];
        [self.contentView addSubview:self.timeLable];
        [_timaImageView release];
        
        
    }
    return self;
    
}

- (void)setPlayingAlbum:(PlayingAlbum *)playingAlbum
{
    if (_playingAlbum != playingAlbum) {
        [_playingAlbum release];
        _playingAlbum = [playingAlbum retain];
    }
    
    _nameLable.text = playingAlbum.title;
    _timeLable.text = [playingAlbum.duration description];
    
    // 时间戳的转换
    // 从1970年1月1日到现在的时间再除以1000 (因为album.lastUptrackAt是微秒), 得到秒数
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:([playingAlbum.duration integerValue] / 1000)];
    // 在NSDate 和 NSString 之间相互转换的格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置一个日期格式,让字符串能按照设定的格式转为相应的日期
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    // 将时间转换为字符串
    NSString *strDate = [formatter stringFromDate:date];
    
    _timeLable.text = [NSString stringWithFormat:@"最近更新 %@", [strDate substringToIndex:10]];
    
  
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.contentView.frame.size.width;
    CGFloat height = self.contentView.frame.size.height;
    
    self.playImageView.frame = CGRectMake(10, height/3, height/3, height/3);
    self.timaImageView.frame = CGRectMake(width, height/3+height/10, height/5, height/5);
    self.timeLable.frame = CGRectMake(width-height/5*3, height/3+height/10, height/5*3, height/5);
    self.nameLable.frame = CGRectMake(15+height/3, height/3, width-(15+height/3+height/5+height/5*3), height/3);
    
}




- (void)awakeFromNib {
    
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
